package pi.gold;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class GerenciamentoDeDados extends SQLiteOpenHelper {

	public static final String nome = "PI - Beta";
	public static final String niveis = "Niveis";
	public static final String nomeDoNivel = "Nome do nivel";
	public static final String nivelCompleto = "Nivel completo";
	public static final String nivelDesbloqueado = "Nivel desbloqueado";
	public static final String pontuacaoDoNivel = "Pontuacao do nivel";

	public GerenciamentoDeDados(Context contexto) {
		// THE VALUE OF 1 ON THE NEXT LINE REPRESENTS THE VERSION NUMBER OF THE DATABASE
		// IN THE FUTURE IF YOU MAKE CHANGES TO THE DATABASE, YOU NEED TO INCREMENT THIS NUMBER
		// DOING SO WILL CAUSE THE METHOD onUpgrade() TO AUTOMATICALLY GET TRIGGERED
		super(contexto, nome, null, 1);
	}

	@Override
	public void onCreate(SQLiteDatabase bancoDeDados) {
		bancoDeDados.execSQL("CREATE TABLE IF NOT EXISTS "+ niveis +" (" + nomeDoNivel + " INTEGER PRIMARY KEY , " + nivelDesbloqueado + " TEXT, " + nivelCompleto + " TEXT, " + pontuacaoDoNivel + " TEXT" + ")");

		//OPTIONALLY PREPOPULATE THE TABLE WITH SOME VALUES   
		ContentValues cv = new ContentValues();
		cv.put(nomeDoNivel, 1);
		cv.put(nivelDesbloqueado, "true");
		cv.put(nivelCompleto, "false");
		cv.put(pontuacaoDoNivel, "0");
		bancoDeDados.insert(niveis, null, cv);
        cv.put(nomeDoNivel, 2);
        cv.put(nivelDesbloqueado, "false");
        cv.put(nivelCompleto, "false");
        cv.put(pontuacaoDoNivel, "0");
        bancoDeDados.insert(niveis, null, cv);
        cv.put(nomeDoNivel, 3);
        cv.put(nivelDesbloqueado, "false");
        cv.put(nivelCompleto, "false");
        cv.put(pontuacaoDoNivel, "0");
        bancoDeDados.insert(niveis, null, cv);
	}

	@Override
	public void onUpgrade(SQLiteDatabase bancoDeDados, int versaoAntiga, int versaoNova) {
		// THIS METHOD DELETES THE EXISTING TABLE AND THEN CALLS THE METHOD onCreate() AGAIN TO RECREATE A NEW TABLE
		// THIS SERVES TO ESSENTIALLY RESET THE DATABASE
		// INSTEAD YOU COULD MODIFY THE EXISTING TABLES BY ADDING/REMOVING COLUMNS/ROWS/VALUES THEN NO EXISTING DATA WOULD BE LOST
		bancoDeDados.execSQL("DROP TABLE IF EXISTS " + niveis);
        onCreate(bancoDeDados);
	}
	
	public String nivelEstaBloqueado(int ID) {
		// THIS METHOD IS CALLED BY YOUR MAIN ACTIVITY TO READ A VALUE FROM THE DATABASE                 
		SQLiteDatabase myDB = this.getReadableDatabase();
		String[] mySearch = new String[]{String.valueOf(ID)};
		Cursor myCursor = myDB.rawQuery("SELECT " + nivelDesbloqueado + " FROM " + niveis + " WHERE " + nomeDoNivel + "=?", mySearch);
		myCursor.moveToFirst();
		int index = myCursor.getColumnIndex(nivelDesbloqueado);
		String myAnswer = myCursor.getString(index);
		myCursor.close();
		return myAnswer;
	}
	
	public int desbloqueiaNivel(int ID, String estaDesbloqueado) {
		//THIS METHOD IS CALLED BY YOUR MAIN ACTIVITY TO WRITE A VALUE TO THE DATABASE          
		SQLiteDatabase bancoDeDados = this.getWritableDatabase();
		ContentValues valores = new ContentValues();
		valores.put(nivelDesbloqueado, estaDesbloqueado);
		int numRowsAffected = bancoDeDados.update(niveis, valores, nomeDoNivel + "=?", new String []{String.valueOf(ID)});
		return numRowsAffected;
    }
}

//PARA COLOCAR NA ACTIVITY

/**/
